<template>
  <div>
    
    <basic-container>
      <el-breadcrumb separator="/"> 
        <el-breadcrumb-item>财务审核</el-breadcrumb-item>
        <el-breadcrumb-item>零钱提现</el-breadcrumb-item>
      </el-breadcrumb>
    </basic-container>
    <basic-container>
      <avue-crud  
        ref="crud"
        :data="data"
        v-model="form"
        :option="option"
        :page.sync="page"
        @on-load="getList"
        @refresh-change="refreshChange"
        @search-reset="from = {}"
        @search-change="searchChange"
        @selection-change="selectionChange">
        <!-- 自定义按钮 -->
        <template slot-scope="scope" slot="menuLeft">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="small"
            plain
            @click.stop="editSelect(3)"
            >通过</el-button
          >
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="small"
            plain
            @click.stop="editSelect(2)"
            >拒绝</el-button
          >
          <el-button
            type="info"
            icon=""
            size="small"
            plain
            @click.stop="editSelect(4)"
            >待定</el-button
          >
          <br />
          <!-- <el-tag> 已通过n条 未通过n条 待审核n条</el-tag> -->
        </template>
        <!-- 自定义操作栏 -->
        <template slot-scope="scope" slot="menu">
          <el-button
            v-if="scope.row.state==1||scope.row.state==4"
            icon="el-icon-edit"
            type="text"
            size="small"
            @click.stop="adopt(scope.row)"
            >通过</el-button
          >
          <el-button
            v-if="scope.row.state==1||scope.row.state==4"
            icon="el-icon-delete"
            type="text"
            size="small"
            @click.stop="refuse(scope.row)"
            >拒绝</el-button
          >
          <el-button
            v-if="scope.row.state==1||scope.row.state==4"
            icon=""
            type="text"
            size="small"
            @click.stop="undetermined(scope.row)"
            >待定</el-button
          >
        </template>
      </avue-crud>
      
    </basic-container>
  </div>
</template>

<script>
import { common } from "@/api/cat/httpRequest";
import { catUrl } from "@/config/env";
import { catRmbExtract as baseURL } from "@/api/cat/baseUrls";
export default {
  data() {
    return {
      page: {
        total: 0,
        pageSize: 10,
        // pageSizes:[1,2,3]
      },
      type: true,
      data: [],
      params: {},
      form: {},
      default: {
        // orderByList: "state&ASC,create_time&DESC",
      },
      // 勾选选中的内容
      checks: [],
      option: {
        delBtn: false,
        editBtn: false,
        searchBtn: true,
        addBtn: false,
        selection:true,
        border: true,
        column: [
          {
            label: "申请时间",
            prop: "create_time",
            type: "daterange",
            search: true,
            required: true,
          },
          {
            label: "用户id",
            prop: "user_id",
            type: "input",
            search: true,
          },
          {
            label: "用户昵称",
            prop: "userName",
            type: "input",
          },
          {
            label: "提现金额",
            prop: "rmb",
            type: "input",
            search: true,
          },
          {
            label: "申请状态",
            prop: "state",
            type: "select",
            search: true,
            dicData:[
              {
                label:"申请中",
                value:1
              },
              {
                label:"拒绝",
                value:2
              },
              {
                label:"成功",
                value:3
              },
              {
                label:"待定",
                value:4
              },
              {
                label:"失败",
                value:5
              }
            ],
            props: {
              label: "label",
              value: "value",
            },
          },
          {
            label: "审核失败原因",
            prop: "reason",
            type: "input",
          },
          {
            label: "累计到账金额",
            prop: "accumulated_cash_return_amount",
            type: "input",
          },
          {
            label: "今日累计提现金额",
            prop: "withdrawal_amount_today",
            type: "input",
          },
          {
            label: "累计申请提现金额",
            prop: "accumulated_withdrawal_amount",
            type: "input",
          },
          {
            label: "累计提现成功次数",
            prop: "successful_withdrawals_number",
            type: "input",
          },
          {
            label: "最后操作信息",
            prop: "examine_time",
            type: "date"
          },
        ],
      },
    };
  },
  methods: {
    
    /**勾选所有 */
    selectionChange(row) {
      this.checks = row;
    },
    //某个不通过
    async refuse(row) {
      await common(baseURL.editState + `/${row.id}/2`, null, null, "GET");
      this.getList();
    },
    //某个通过
    async adopt(row) {      
      await common(baseURL.editState + `/${row.id}/3`, null, null, "GET");
      this.getList();
    },
    //某个待定
    async undetermined(row){
      await common(baseURL.editState + `/${row.id}/4`, null, null, "GET");
      this.getList();
    },
    /**
     * 全选的通过或拒绝
     * type : 1表示通过,2表示拒绝,3表示待定
     */
    async editSelect(type) {
      if (this.checks.length == 0) {
        return this.$message.error("请选择内容");
      }
      let arr = this.checks.filter((item) => item.state == type);
      let arr2 = this.checks.filter(item => item.state==2 || item.state==3);
      if(arr2.length>0){
        return this.$message.error("不可修改已通过或拒绝的内容");
      } 
      if (arr.length == 0) { 
        let arr3 = this.checks.filter(item => item.state==5);
        if(arr3.length>0){
          return this.$message.error("不可修改失败状态的内容");
        }
        let userInfo = this.$store.state.user.userInfo
        this.checks.forEach((item) => {
          item.state = type;
          item.edit_account_name = userInfo.username;
          item.examine_time = new Date();
        });
        await common(baseURL.editStateAll, this.checks, null, "POST");
        // this.checks = []
      } else {
        return this.$message.error("不可修改相同类型章节");
      }
    },


    /**获取列表 */
    async getList() {
      this.loading = true;
      //清空表格列表
      this.data = [];
      //发送请求获取数据
      let response = await common(
        baseURL.pageAndParam,
        {...this.params,...this.default},
        this.page,
        "POST"
      );
      this.page = response.page;
      this.data = response.data;
      this.loading = false;
    }, 
    /**刷新 */
    refreshChange() {
      this.getList();
    }, 
    /**搜索 */
    searchChange(params, done) {
      
      params.create_time = `${params.create_time ? "!<>!" + params.create_time : ""}`
      this.params = params;
      this.page.currentPage = 1;
      this.getList();
      if (done) done();
    },
  },
};
</script>